define (require, exports, module) ->
  # 引入页面样式
  require 'mods/sygg/sygg_css.css'
  # 引入API服务
  sygg_api = require 'mods/sygg/sygg_api'

  # 引入获取终端类型
  {getDeviceType} = require "js/device_type"

  module.exports = Vue.extend

    # 组件模板
    template: require 'mods/sygg/sygg_tpl.js'

    data: () ->
      # 弹出公告的显示与否控制变量
      show_ads: false
      # 广告列表
      sygg_list: []
      # 定时器
      timer: null
      # 终端类型:
      deviceType: getDeviceType()

    methods:

      # 判断是否在显示时间内
      getGgStatu:(d1, d2)->

        # 产品状态
        statu = false

        # 获取当前日期
        curdate = new Date()
        y = curdate.getFullYear()
        m = curdate.getMonth() + 1
        d = curdate.getDate()
        if m < 10
          m = '0' + m
        d0 = y + '-' + m + '-' + d

        # 将字符串日期转换为可比较
        time0 = new Date(d0?.replace("-", "/").replace("-", "/"));
        time1 = new Date(d1?.replace("-", "/").replace("-", "/"));
        time2 = new Date(d2?.replace("-", "/").replace("-", "/"));

        # 当前日期在开始和结束日期之间
        if time0 >= time1 and time0 <= time2
          statu = true

        return statu

      # 获取弹出广告的内容
      getPopAds: () ->
        self = @
        # 查询广告的参数
        params =
          pageNum: 1
          pageSize: 10

        return sygg_api.getPopAds params
          .then (res) ->
            itemList = res?.body?.data
            for item in itemList
              statu = self.getGgStatu item.begin_date, item.end_date
              if 'PC' isnt self.deviceType
                if 'dialog_mb' is item.gg_type && statu
                  # 过滤掉移动端弹窗图片
                  if not item.hasOwnProperty 'img_addr'
                    self.sygg_list.push item
              else if 'dialog_pc' is item.gg_type && statu
                self.sygg_list.push item

            # 弹出广告轮播
            self.popAdsCarouse()

          .catch (err) ->
            console.log err

      # 弹出广告轮播
      popAdsCarouse: () ->
        self = @
        # 幻灯片个数
        gtop_size = self.sygg_list.length
        #第几个幻灯片
        idx = 1
        # 幻灯片定时器
        timer = null

        # 幻灯片启动方法
        start = ()->
          self.timer = setInterval ()->
            idx++
            #如果不是最后一张，则每5s设置图片淡入，并将导页码选中
            if idx <= gtop_size
              self.playCurrgtop idx
            #如果是最后一张，则重新回到第一张，并将页码选中
            else
              self.playCurrgtop 1
              idx = 1
          , 4800

        # 幻灯片关闭方法
        stop = ()->
          if timer
            # 清除定时器
            clearInterval timer
            timer = null

        # 调用弹出广告(自动播放)
        start idx

      # 关闭定时器
      closeInterval: () ->
        self = @
        if self.timer
          clearInterval self.timer
          self.timer = null

      # 给每一页广告的鼠标移出绑定事件
      contMouseLeave: (idx) ->
        self = @
        # 幻灯片个数
        gtop_size = self.sygg_list.length
        #第几个幻灯片
        idx = 1
        # 幻灯片定时器
        timer = null

        # 幻灯片启动方法
        start = ()->
          self.timer = setInterval ()->
            idx++
            #如果不是最后一张，则每5s设置图片淡入，并将导页码选中
            if idx <= gtop_size
              self.playCurrgtop idx
            #如果是最后一张，则重新回到第一张，并将页码选中
            else
              self.playCurrgtop 1
              idx = 1
          , 4800

        # 调用弹出广告(自动播放)
        start idx

      # 为页码绑定鼠标移入和点击事件
      mouseEvent: (idx) ->
        self = @
        self.playCurrgtop idx + 1

      # 点击改变下标
      changeIdx: (idx) ->
        self = @
        self.playCurrgtop idx + 1

      # 播放第几个广告
      playCurrgtop: (currIdx) ->
        self = @
        gtop_size = self.sygg_list.length
        currIdx = currIdx-1
        for i in [0...gtop_size]
          if i != currIdx
            self.showCurrgtop i, false
          else
            self.showCurrgtop i, true

      # 显示/隐藏第几个图片
      showCurrgtop: (idx, showFlag) ->
        gtopItem = $("#gtop_item_index_" + idx)
        gtopTri = $("#gtop_trigger_index_" + idx)
        if showFlag
          gtopItem.fadeIn 'slow'
          # gtopItem.css "display", "block"
          gtopTri.addClass 'selected'
        else
          gtopItem.finish().fadeOut 'slow'
          # gtopItem.css "display","none"
          gtopTri.removeClass 'selected'

      # cookie -- 弹出广告的显示与隐藏
      isHasCookie: () ->
        self = @
        # 获取cookie值
        cookieString = new String document.cookie
        # 设置头部
        cookieHeader = "notdialog="
        beginPosition = cookieString.indexOf cookieHeader

        # 如果没有cookie值，则弹出广告弹出框
        if beginPosition == -1 && 0 isnt self.sygg_list?.length
          self.show_ads = true  # 显示广告弹出框

      # 关闭弹出框
      closePopAds: () ->
        self = @
        # 删除cookie
        time = new Date()
        time.setTime time.getTime() + 24 * 60 * 60 * 1000
        # 定义cookie值
        document.cookie = 'notdialog=newdialog;expires=' + time.toGMTString()
        # 隐藏弹出广告
        self.show_ads = false

    mounted: () ->
      self = @
      # 获取弹出广告
      self.getPopAds()
        .then (res) ->
          # 通过cookie和弹窗数据判断是否显示弹窗
          self.isHasCookie()
        .catch (error) ->
          console.error 'error:',error
      return